<?php
/**
 * 评价
 * @author maniac
 */
namespace app\api\controller\v1;

use app\facade\Evaluates AS EvaluatesFacade;

class Evaluates extends Base
{
    /**
     * 获取评价列表
     */
    public function evaluates()
    {
        $userToken = input('post.userToken/s', '');
        $userid    = 0;
        if ($userToken) {
            $userid = $this->parseUserToken($userToken);
        }
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        $goods_id = input('post.goods_id/d', 10);
        $offset = ($page - 1)*$limit;
        $limit = $offset.','.$limit;
        try {
            $where=[];
            if($goods_id){
                $where[] = [
                    ['goods_id', 'eq', $goods_id],
                ];
            }

            if($userid){
                $where[] = [
                    ['userid', 'eq', $userid],
                ];
            }

            $result = EvaluatesFacade::evaluates($where, $limit);
            if ($result) {
                return j(0, '获取评价列表成功', $result);
            }
            return j(1, '获取评价列表失败');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
    
    /**
     * 添加评价
     */
    public function add()
    {
        $post = input('post.');
        $post['userid'] = $this->checkUserToken();
        if (!$post['level'] || !$post['content']) {
            return j(1, '请完善评价内容');
        }
        try {
            $result = EvaluatesFacade::add($post);
            if ($result) {
                return j(0, '评价成功');
            }
            return j(1, '评价失败');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
}
